package tr.edu.yildiz.ce.app_manager.dao.impl.hibernate;

import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;

import tr.edu.yildiz.ce.app_manager.dao.ProxyDao;
import tr.edu.yildiz.ce.app_manager.model.AppProxy;

@Repository
public class ProxyDaoImpl extends BasicDaoImpl<AppProxy,Long> implements ProxyDao{

	@Override
	public AppProxy findByUsernameAndPassword(String username, String password) {
		CriteriaBuilder queryBuilder = getEntityManager().getCriteriaBuilder();
		CriteriaQuery qdef = queryBuilder.createQuery();
		Root<AppProxy> proxyQuery = qdef.from(AppProxy.class);
		Predicate condUsername = queryBuilder.equal(proxyQuery.get("PROXY_USERNAME"), username);
		Predicate condPassword = queryBuilder.equal(proxyQuery.get("PROXY_PASSWORD"), password);
		qdef.where(condUsername,condPassword);
		Query query = getEntityManager().createQuery(qdef);
		return (AppProxy) query.getSingleResult();
	}

}
